package top.yxqz.req;

import org.apache.commons.beanutils.BeanUtils;
import top.yxqz.school.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;

/**
 * @author Mr.余
 * @date 2022/11/22
 * @package top.yxqz.req
 * 得到客户端中的所有Cookie
 */

@WebServlet("/demo5")
public class ReqDemo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        method1(request);
//        method2(request);

        method3(request);


    }

    /**
     * 使用BeanUtils工具类（推荐）
     * @param request
     */
    private void method3(HttpServletRequest request) {
        Student student   = new Student();
        try {
//            tomcat8.5一下，需要导commons-logging包
            BeanUtils.populate(student, request.getParameterMap());
            System.out.println(student);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方式二，使用反射以及属性描述器来存储数据
     * @param request
     */
    private void method2(HttpServletRequest request) {
        Student student = new Student();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = request.getParameter(parameterName);

            try {
//                创建属性描述器（JDK）
                PropertyDescriptor descriptor = new PropertyDescriptor(parameterName, Student.class);
//                得到读取方法
                Method writeMethod = descriptor.getWriteMethod();
//                向容器中存储数据
                writeMethod.invoke(student,parameterValue);

                System.out.println(student);
            } catch (IntrospectionException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 方式一，通过对象的方式来封装数据
     * @param request
     */
    private void method1(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
//      方式一：  使用对象来封装前端的请求数据
        Student student = new Student(username, password);
    }
}
